package com.dqp.cslggroup.ConnectSever;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.Comment;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.verification;
import com.dqp.cslggroup.bean.Club;
import com.dqp.cslggroup.bean.Talk;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;


public class Accept {
    private static Connection.Response response;
    static String ip = SharedPreferencesUtil.getIp(MyApplication.getContext());

    public static ArrayList<Club> schoolImg() {
        ArrayList<Club> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/app/schoolImg.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray schoolimg = JSON.parseArray(jsonObject.getString("schoolimg"));
                if (!schoolimg.isEmpty()) {
                    for (Object o : schoolimg) {
                        JSONObject img = (JSONObject) o;
                        Club club = new Club();
                        club.setClub_img(img.getString("path"));
                        club.setClub_name(img.getString("title"));
                        dataList.add(club);
                    }
                }
            }
        } catch (IOException | Error e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static ArrayList<Club> getClub(int count) {
        ArrayList<Club> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/club/club.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("count", count + "")
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("club"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Club club = new Club();
                        club.setClub_name(x.getString("name"));
                        club.setClub_des(x.getString("des"));
                        club.setClub_img(x.getString("img"));
                        club.setClub_bad(x.getIntValue("bad"));
                        club.setClub_level(x.getString("level"));
                        club.setClub_fine(x.getIntValue("fine"));
                        club.setClub_comment(x.getIntValue("comment"));
                        club.setShow1(x.getString("show1"));
                        club.setShow2(x.getString("show2"));
                        club.setShow3(x.getString("show3"));
                        dataList.add(club);
                    }

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static ArrayList<Comment> getClubComment(String community) {
        ArrayList<Comment> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/club/getComment.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("community", community)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("comment"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Comment comment = new Comment();
                        comment.setComment_name(x.getString("name") + "同学");
                        comment.setCollege(x.getString("college"));
                        comment.setContent(x.getString("content"));
                        comment.setDate(x.getString("dates"));
                        comment.setImg(x.getString("img"));
                        comment.setGender(x.getString("gender"));
                        dataList.add(comment);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static ArrayList<Comment> getTalkComment(String id, int count) {
        ArrayList<Comment> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/talkComment.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("talkid", id)
                    .data("count", count + "")
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("comment"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Comment comment = new Comment();
                        comment.setComment_name(x.getString("name") + "同学");
                        comment.setContent(x.getString("content"));
                        comment.setDate(x.getString("dates"));
                        comment.setImg(x.getString("img"));
                        comment.setGender(x.getString("gender"));
                        comment.setCollege(x.getString("college"));
                        dataList.add(comment);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static int blacklist(String sno) {
        int grade = 0;
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/app/blacklist.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("sno", sno)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("blacklist"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        grade = (x.getIntValue("grade"));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return grade;
    }

    public static ArrayList<Talk> getTalk(int count) {
        ArrayList<Talk> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/campustalk.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("count", count + "")
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("campustalk"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Talk talk = new Talk();
                        talk.setName(x.getString("name") + "同学");
                        talk.setContent(x.getString("content"));
                        talk.setImg(x.getString("img"));
                        talk.setDates(x.getString("dates"));
                        talk.setBad(x.getIntValue("bad"));
                        talk.setFine(x.getIntValue("fine"));
                        talk.setComment(x.getIntValue("comment"));
                        talk.setId(x.getIntValue("id"));
                        talk.setHead(x.getString("head"));
                        talk.setGender(x.getString("gender"));
                        talk.setSum(x.getIntValue("sum"));
                        dataList.add(talk);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static ArrayList<Comment> getProfession(String profession) {
        ArrayList<Comment> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/profession.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("college", profession)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("profession"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Comment comment = new Comment();
                        comment.setComment_name(x.getString("name"));
                        comment.setCollege(x.getString("college"));
                        comment.setContent(x.getString("des"));
                        comment.setDate(x.getString("features"));
                        dataList.add(comment);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }


    public static ArrayList<Club> getCollege(int count) {
        ArrayList<Club> dataList = new ArrayList<>();
        try {
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/collegeSel.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .data("count", count + "")
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray clubs = JSON.parseArray(jsonObject.getString("college"));
                if (!clubs.isEmpty()) {
                    for (Object o : clubs) {
                        JSONObject x = (JSONObject) o;
                        Club club = new Club();
                        club.setClub_name(x.getString("name"));
                        club.setClub_img(x.getString("ico"));
                        club.setClub_des(x.getString("des"));
                        club.setClub_level(x.getString("web"));
                        club.setClub_fine(x.getIntValue("profession"));
                        club.setClub_bad(x.getIntValue("pv"));
                        club.setShow1(x.getString("show1"));
                        club.setShow2(x.getString("show2"));
                        club.setShow3(x.getString("show3"));
                        dataList.add(club);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    public static String getSchoolCalendar() {

        try {
            Connection.Response response;
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/calendar.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray appVersion = JSON.parseArray(jsonObject.getString("calendar"));
                if (appVersion != null && !appVersion.isEmpty()) {
                    Iterator iterator = appVersion.iterator();
                    JSONObject version = (JSONObject) iterator.next();
                    return version.getString("calendar");
                }
            }
        } catch (IOException | Error e) {
            e.printStackTrace();
            return null;
        }
        return "0";
    }

    public static int getdakaNormal() {
        try {
            Connection.Response response;
            response = Jsoup.connect("http://210.28.164.3/47.96.190.22/school/daka.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray appVersion = JSON.parseArray(jsonObject.getString("daka"));
                if (appVersion != null && !appVersion.isEmpty()) {
                    Iterator iterator = appVersion.iterator();
                    JSONObject version = (JSONObject) iterator.next();
                    return version.getIntValue("normal");
                }
            }
        } catch (IOException | Error e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    public static JSONObject getDKdata(String userid) {
        try {
            response = Jsoup.connect("https://4.ldgnet.cslg.edu.cn/api/api.php")
                    .data(verification.digitalSignature(19))
                    .data("userId", userid)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(5000)
                    .execute();
            if (response.body() != null && response.body().contains("{"))
                return JSON.parseObject(response.body());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }


}
